/**
 * 添加商品进入购物车事件
 */
function addEvent(index, shoppingCart) {
  var newShoppingCart = shoppingCart
  newShoppingCart.list[index].num = newShoppingCart.list[index].num + 1
  newShoppingCart.count = newShoppingCart.count + 1
  newShoppingCart.allPrice = newShoppingCart.allPrice +     newShoppingCart.list[index].data.price
  return newShoppingCart
}

/**
 * 从购物车删除商品事件
 */
function minusEvent(index, shoppingCart) {
  var newShoppingCart = shoppingCart
  if (newShoppingCart.list[index].num > 0) {
    newShoppingCart.list[index].num = newShoppingCart.list[index].num - 1
    newShoppingCart.count = newShoppingCart.count - 1
    newShoppingCart.allPrice = newShoppingCart.allPrice - newShoppingCart.list[index].data.price
  }
  return newShoppingCart
}
function getCartType(merchant_id, type){
  return merchant_id + '_' + type
}
function saveCart(merchant_id, type, cartData){
    var cartType = getCartType(merchant_id, type)
    wx.setStorageSync(cartType, JSON.stringify(cartData))
}
function getCart(merchant_id, type){
  var cartType = getCartType(merchant_id, type)
  return wx.getStorageSync(cartType)
}

module.exports = {
  addEvent: addEvent,
  minusEvent: minusEvent
}
